package com.renren.webmaven2.service;

import java.sql.Timestamp;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.renren.eliya.domain.BigClass;
import com.renren.eliya.domain.SmallClass;
import com.renren.yx.base.dao.daoutil.InitDao;

/**
 * write
 * 
 * @author jizhong.yang
 * @version V1.0
 */

@Service
@SuppressWarnings("unchecked")
public class BigClassService {

	@Autowired
	private MonitorService monitorService;

	@Autowired
	private SmallClassService smallClassService;

	public int add(String name, String weight) {
		try {
			BigClass bigClass = new BigClass();
			bigClass.setName(name);
			bigClass.setWeight(Integer.valueOf(weight));
			bigClass.setCreateTime(new Timestamp(System.currentTimeMillis()));
			InitDao.getDao().addEntity(bigClass);
			monitorService.getBigClasses();
			return 1;
		} catch (Exception e) {
			return 0;
		}
	}

	public int del(Integer id) {
		try {
			BigClass bigClass = (BigClass) InitDao.getDao().getEntity(
					BigClass.class, id);
			InitDao.getDao().delEntity(bigClass);
			monitorService.getBigClasses();

			List<SmallClass> smallClasses = smallClassService
					.findByBigClass(id);
			for (SmallClass smallClass : smallClasses) {
				InitDao.getDao().delEntity(smallClass);
			}
			monitorService.getSmallClasses();
			return 1;
		} catch (Exception e) {
			return 0;
		}
	}

	public int update(String bigClassName, Integer weight, Integer id) {
		try {
			BigClass bigClass = (BigClass) InitDao.getDao().getEntity(
					BigClass.class, id);
			bigClass.setName(bigClassName);
			bigClass.setWeight(weight);
			InitDao.getDao().updateEntity(bigClass);
			monitorService.getBigClasses();
			List<SmallClass> smallClasses = smallClassService
					.findByBigClass(id);
			for (SmallClass smallClass : smallClasses) {
				smallClass.setBigClassName(bigClassName);
				smallClass.setCreateTime(new Timestamp(System
						.currentTimeMillis()));
				InitDao.getDao().updateEntity(smallClass);
			}
			monitorService.getSmallClasses();
			return 1;
		} catch (Exception e) {
			return 0;
		}
	}

	public BigClass get(Integer bigClassId) {
		return null;
	}

}
